package com.yonyou.gmmc.common.sms;

import java.util.Date;

import org.apache.log4j.Logger;
import org.marketing.common.FrameHttpUtil;
import org.marketing.common.dto.sms.TemplateSMS;
import org.marketing.common.util.DateUtil;
import org.marketing.common.util.MD5Util;
import org.springframework.beans.factory.annotation.Value;

import com.google.gson.Gson;

public class SmsHttpClient2 {
	
	private static Logger logger = Logger.getLogger(SmsHttpClient2.class);
	
	@Value("${SMS_PWD}")
    private String                   SMS_PWD;
    @Value("${SMS_BASEURL2}")
    private String                   SMS_BASEURL2;
	
	public static String sendSms(String smsPass, String smsKey, String SMS_TEMPLATEID,
			String phone, String content){
		String result = "";
		/*CloseableHttpClient httpclient = AbsRestClient.getDefaultHttpClient();
		try{*/
		try {
			String SMS_BASEURL2 = "https://api.ucpaas.com";
			String SMS_VERSION = "2014-06-30";
			String SMS_ACCOUNTSID = "b630a458b05492c35732abace72692b3";
			String SMS_AUTHTOKEN = "789e842fb53baaa1ac7e8673d9190ce9";
			String SMS_APPID = "014db0a981524914830a2588a0382620";
			logger.info("发送短信:smsPass"+smsPass+",smsKey:"+smsKey
					+",SMS_BASEURL2:"+SMS_BASEURL2+",SMS_VERSION:"+SMS_VERSION
					+",SMS_ACCOUNTSID:"+SMS_ACCOUNTSID+",SMS_AUTHTOKEN:"+SMS_AUTHTOKEN
					+",SMS_APPID:"+SMS_APPID+",SMS_TEMPLATEID:"+SMS_TEMPLATEID
					+",phone:"+phone+",content:"+content);
			if (smsPass.equals("YONYOU") && smsKey.equals("GMMC")){
				
	            // 构造请求URL内容
	            String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);// 时间戳
	            String sig = SMS_ACCOUNTSID + SMS_AUTHTOKEN + timestamp;
	            logger.info("发送短信sig:"+sig);
	            String signature2 = MD5Util.MD5(sig);
	            String signature = signature2.toUpperCase();
	            logger.info("发送短信signature:"+signature);
	            //String signature = AbsRestClient.getSignature(SMS_ACCOUNTSID, SMS_AUTHTOKEN, timestamp, encryptUtil);
	            SMS_BASEURL2 = SMS_BASEURL2 + "/" + SMS_VERSION + "/Accounts/" 
	            			+ SMS_ACCOUNTSID + "/Messages/templateSMS?sig=" + signature;
	            logger.info("发送短信URL:"+SMS_BASEURL2);
	            TemplateSMS templateSMS = new TemplateSMS();
	            templateSMS.setAppId(SMS_APPID);
	            templateSMS.setTemplateId(SMS_TEMPLATEID);
	            templateSMS.setTo(phone);
	            templateSMS.setParam(content);
	            Gson gson = new Gson();
	            String body = gson.toJson(templateSMS);
	            body = "{\"templateSMS\":" + body + "}";
	            logger.info("发送短信body:"+body);
	            String src = SMS_ACCOUNTSID + ":" + timestamp;
	            logger.info("发送短信src:"+src);
	            String auth = MD5Util.base64Encoder(src);//MD5加密
	            logger.info("发送短信auth:"+auth);
	            result = FrameHttpUtil.postJson2(SMS_BASEURL2,body,auth);
	            logger.info("发送短信result:"+result);
	            /*HttpEntity entity = response.getEntity();
	            if (entity != null) {
	                result = EntityUtils.toString(entity, "UTF-8");
	            }
	            EntityUtils.consume(entity);*/
			}else{
				result = "信息不完整";
	        }
		/*}catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
        	try {
				httpclient.close();
			} catch (IOException e) {
				logger.error("异常" + e.getMessage(), e);
			}
        }*/
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
}
